#!/bin/sh

DAEMON=suricata
PIDFILE=/var/run/$DAEMON.pid

SURICATA_ARGS="-c /etc/suricata/suricata.yaml -i eth0"

SURICATA_RELOAD=0

[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"

start() {
	printf 'Starting %s: ' "$DAEMON"
	mkdir -p /var/log/suricata
	start-stop-daemon -b -m -S -q -p "$PIDFILE" -x "/usr/bin/$DAEMON" \
		-- $SURICATA_ARGS
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

stop() {
	printf 'Stopping %s: ' "$DAEMON"
	start-stop-daemon -K -q -p "$PIDFILE"
	status=$?
	if [ "$status" -eq 0 ]; then
		rm -f "$PIDFILE"
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

restart() {
	stop
	sleep 1
	start
}

# SIGUSR2 makes suricata reload rules
reload() {
	printf 'Reloading %s: ' "$DAEMON"
	start-stop-daemon -K -s "$SURICATA_RELOAD" -q -p "$PIDFILE"
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

case "$1" in
	start|stop|restart|reload)
		"$1";;
	*)
		echo "Usage: $0 {start|stop|restart|reload}"
		exit 1
esac
